package cn.nirvana.aloha;

import cn.nirvana.aloha.controller.PersonEditDialogController;
import cn.nirvana.aloha.model.Person;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Stage primaryStage;
    private static Scene scene;
    private BorderPane rootLayout;


    @Override
    public void start(Stage stage) throws IOException {
        this.primaryStage = stage;
        this.primaryStage.setTitle("Nirvana Studio");
        // Set the application icon.
        String path = App.class.getResource("images/orange_icon.png").toExternalForm();
        this.primaryStage.getIcons().add(new Image(path));

        initRootLayout();

        showKeysConfig();
    }

    /**
     * Initializes the root layout.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            rootLayout = (BorderPane) loadFXML("RootLayout");
            // Show the scene containing the root layout.
            scene = new Scene(rootLayout);
            scene.getStylesheets().add(getClass().getResource("css/DarkTheme.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public void showKeysConfig() {
        try {
            AnchorPane keysConfig = (AnchorPane) loadFXML("KeysConfig");
            rootLayout.setCenter(keysConfig);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * Shows the person overview inside the root layout.
     */
    public void showPersonOverview() throws IOException {
        try {
            // Load person overview.
            AnchorPane personOverview = (AnchorPane) loadFXML("PersonOverview");

            // Set person overview into the center of root layout.
            rootLayout.setCenter(personOverview);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean showPersonEditDialog(Person person) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(App.class.getResource("PersonEditDialog.fxml"));
            AnchorPane dialog = fxmlLoader.load();

            // Create the dialog Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Edit Person");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(dialog);
            dialogStage.setScene(scene);

            PersonEditDialogController controller = fxmlLoader.getController();
            controller.setDialogStage(dialogStage);
            controller.setPerson(person);

            // Show the dialog and wait until the user closes it
            dialogStage.showAndWait();

            return controller.isOkClicked();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * Opens a dialog to show birthday statistics.
     */
    public static void showBirthdayStatistics() {
        try {
            // Load the fxml file and create a new stage for the popup.
            AnchorPane page = (AnchorPane) loadFXML("BirthdayStatistics");
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Birthday Statistics");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

            dialogStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Stage getPrimaryStage() {
        return primaryStage;
    }


    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }


    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }


    public static void main(String[] args) {
        launch();
    }

}